//
//  UIKeyboardView.m
//
//
//  Created by  YFengchen on 13-1-4.
//  Copyright 2013 __zhongyan__. All rights reserved.
//

#import "UIKeyboardView.h"


#define ColorOfHex(value) [UIColor colorWithRed:((value&0xFF0000)>>16)/255.0 green:((value&0xFF00)>>8)/255.0 blue:(value&0xFF)/255.0 alpha:1.0]


@implementation UIKeyboardView

@synthesize array;
@synthesize delegate = _delegate;

//从bundle中获取图片

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
		keyboardToolbar = [[UIToolbar alloc] initWithFrame:frame];
		
//		keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
        
		keyboardToolbar.backgroundColor = ColorOfHex(0xf0f1f2);//lv改
        
        [self addSubview:keyboardToolbar];
		
//        UIBarButtonItem *previousBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"previous", @"")
//																			style:UIBarButtonItemStyleBordered
//																		   target:self
//																		   action:@selector(toolbarButtonTap:)];
//        UIBarButtonItem * previousBarItem = [[UIBarButtonItem alloc]initWithImage:[self imageNamedFrombundle:@"LeftArrowB"] style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarButtonTap:)];
//		previousBarItem.tag=1;//lv改
		
//		UIBarButtonItem *nextBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"next", @"")
//																		style:UIBarButtonItemStyleBordered
//																	   target:self
//																	   action:@selector(toolbarButtonTap:)];
//        UIBarButtonItem * nextBarItem = [[UIBarButtonItem alloc]initWithImage:[self imageNamedFrombundle:@"RightArrowB"] style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarButtonTap:)];//lv改
//		nextBarItem.tag=2;
//		
//		UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItem.SystemItemFlexibleSpace
//																					  target:nil
//																					  action:nil];
//		
//		UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"完成", @"")
//																		style:UIBarButtonItemStyleDone
//																	   target:self
//																	   action:@selector(toolbarButtonTap:)];//lv改
//		doneBarItem.tag=3;
//		[doneBarItem setTintColor:ColorOfHex(0x000000)];//lv改
//        
//		[keyboardToolbar setItems:[NSArray arrayWithObjects:previousBarItem, nextBarItem, spaceBarItem, doneBarItem, nil]];
//		[previousBarItem release];
//		[nextBarItem release];
//		[spaceBarItem release];
//		[doneBarItem release];
//		[self addSubview:keyboardToolbar];
//		[keyboardToolbar release];
        //******lv改*********
//        UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0.5, self.frame.size.width, 0.5)];
//        lineView.backgroundColor = ColorOfHex(0x939599);
//        [self addSubview:lineView];
        
        UIButton * previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
        previousButton.frame = CGRectMake(15, 12.5, 11.5, 19);
        [previousButton setBackgroundImage:[UIImage imageNamed:@"LeftArrowG"] forState:UIControlStateDisabled];
        [previousButton setBackgroundImage:[UIImage imageNamed:@"LeftArrowB"] forState:UIControlStateNormal];
        [previousButton addTarget:self action:@selector(toolbarButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [previousButton setEnabled:NO];
        previousButton.tag = 1;
        [self addSubview:previousButton];
        
        UIButton * nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        nextButton.frame = CGRectMake(55.5, 12.5, 11.5, 19);
        [nextButton setBackgroundImage:[UIImage imageNamed:@"RightArrowG"] forState:UIControlStateDisabled];
        [nextButton setBackgroundImage:[UIImage imageNamed:@"RightArrowB"] forState:UIControlStateNormal];
        [nextButton addTarget:self action:@selector(toolbarButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        nextButton.tag = 2;
        [self addSubview:nextButton];
        
        UIButton * doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(self.frame.size.width-49, 12.5,40, 19);
        [doneButton setTitle:@"完成" forState:UIControlStateNormal];
        [doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
        [doneButton setTitleColor:ColorOfHex(0x000000) forState:UIControlStateNormal];
        [doneButton setTitleColor:ColorOfHex(0x888888) forState:UIControlStateHighlighted];
        [doneButton addTarget:self action:@selector(toolbarButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        doneButton.tag = 3;
        [self addSubview:doneButton];
        
        array = [[NSArray alloc]initWithObjects:previousButton,nextButton,doneButton, nil];
        //******lv改*********
    }
    return self;
}


- (void)toolbarButtonTap:(UIButton *)button {
	if ([self.delegate respondsToSelector:@selector(toolbarButtonTap:)]) {
		[self.delegate toolbarButtonTap:button];
	}
}

@end

@implementation UIKeyboardView (UIKeyboardViewAction)
/* 原
//根据index找出对应的UIBarButtonItem
- (UIBarButtonItem *)itemForIndex:(NSInteger)itemIndex {
	if (itemIndex < [[keyboardToolbar items] count]) {
		return [[keyboardToolbar items] objectAtIndex:itemIndex];
	}
	return nil;
}
*/

//****lv改 ****
//根据index找出对应的UIBarButtonItem
- (UIButton *)itemForIndex:(NSInteger)itemIndex {
	if (itemIndex < [array count]) {
		return [array objectAtIndex:itemIndex];
	}
	return nil;
}

@end
